// sh_func.lua
// Houses the most important functions server-side

function TimeSurvivedUpdate()
	local allply = player.GetAll()
	for k, v in pairs(allply) do
	if(v:GetNetworkedEntity("gb_core"):IsValid()) then
		v:SetNetworkedInt("gb_time", v:GetNetworkedInt("gb_time") + 1)
	end
	end
end

function ChangeRound(round)
	gb_CurrentRound = round

	local rf = RecipientFilter()
	rf:AddAllPlayers()

	umsg.Start("changeround", rf)
		umsg.Long(round)
	umsg.End()
end

function NoUndo( player, command, args )
	player:PrintMessage( HUD_PRINTTALK, "Sorry, but undo is disabled during fight round." )
end

function StartFight()
	ChangeRound(2)
	gb_RoundTimer = gb_FightTime

	// Put everyone in spectator mode
	local allplayers = player.GetAll()
	for k, v in pairs(allplayers) do
		// Put them into spectator mode
		local cam = v:GetNetworkedEntity("gb_cam")

		if(cam:IsValid()) then
			v:Spectate( OBS_MODE_CHASE )
			v:SpectateEntity(cam)
			v:StripWeapons()
			v:SetNetworkedBool("spectating", true)

		else
			v:Spectate( OBS_MODE_ROAMING )
			v:StripWeapons()
			v:SetNetworkedBool("spectating", false)
		end
	end

	// Teleport all the robots into the arena
	robotspawns = ents.FindByClass("info_robot")

	for k, v in pairs(allplayers) do
		local core = v:GetNetworkedEntity("gb_core")
		if(core:IsValid()) then
			local randomspawn = math.random(#robotspawns)
			local spawnent = table.remove(robotspawns, randomspawn)
			local spawnpos = spawnent:GetPos()
			local corepos = core:GetPos()
			local const = constraint.GetAllConstrainedEntities(core)
			for _, Ent in pairs(const) do
				local phys1 = Ent:GetPhysicsObject()
				if phys1 and phys1:IsValid() then
					phys1:EnableMotion( true )
					phys1:Wake()
					phys1:ApplyForceCenter( Vector( 0, 0, -1) )
				end
				Ent:SetPos((Ent:GetPos() - corepos + spawnpos))
			end
			core:SetPos(spawnent:GetPos())
			local phys = core:GetPhysicsObject()
			if phys and phys:IsValid() then
				phys:EnableMotion( true )
				phys:Wake()
				phys:ApplyForceCenter( Vector( 0, 0, -1) )
			end
		end
	end
	concommand.Add("undo", NoUndo )
	concommand.Add("gmod_undo", NoUndo )
	concommand.Add("gmod_undonum", NoUndo )
	timer.Create("checkcores", 1, 0, CheckCores)
	timer.Create("timesurvived", 1, 0, TimeSurvivedUpdate)
end

function CheckCores()
	if (gb_NumBlueCores == 0 || gb_NumRedCores == 0) then
		GameOver()
	end
end

function Changethemap()
	game.LoadNextMap()
end

function Showbest()
	local longest = 0
	local longestply = {}
	for k, v in pairs(player.GetAll()) do
		if(v:GetNetworkedInt("gb_time") > longest) then
			longest = v:GetNetworkedInt("gb_time")
			longestply = v
		end
	end
 
	local bestmsg = "The longest surviving player is " .. longestply:Nick() .. "!"
	for k, v in pairs(player.GetAll()) do
		v:PrintMessage(HUD_PRINTCENTER, bestmsg)
	end
	timer.Simple(10, Changethemap)
	timer.Remove("timesurvived")
end

function GameOver(winteam)
	ChangeRound(3)

	if winteam then //override win
		if winteam == 0 then
			winmsg = "It's a tie!"
		elseif winteam == 1 then
			winmsg = "Red team wins!"
		elseif winteam == 2 then
			winmsg = "Blue team wins!"
		end
	else //normal win
		if gb_NumRedCores > gb_NumBlueCores then
			// Red team wins!
			winmsg = "Red team wins!"
		elseif gb_NumRedCores < gb_NumBlueCores then
			// Blue team wins!
			winmsg = "Blue team wins!"
		elseif gb_NumRedCores == gb_NumBlueCores then
			// It was a tie! D:
			winmsg = "It's a tie!"
		end
	end

	for k, v in pairs(player.GetAll()) do
		v:PrintMessage(HUD_PRINTCENTER, winmsg)
	end

	timer.Simple(5, Showbest)
	timer.Remove("RoundTimer")
	timer.Remove("checkcores")
end
